<template>
  <div>
    <div ref="stage"></div>
    <button @click="http">网络请求测试</button>
    <button @click="vuex">vuex测试</button>
  </div>
</template>
  
<script>
import { canvas, main_1 } from "panghu-planebattle-esm"
export default {
  data() {
    return {
      player: {
        id: '',
        loginName: null,
        life: null,
        score: null,
      },
      count: 1,
    }
  },
  methods: {
    http() {
      setInterval(() => {
        this.axios.post('http://localhost:3312/sys-user/update', this.player)
          .then((resp) => {

            console.log("this is update", resp);
            let data = resp.data;
            //
            if (data.success) {
              console.log({
                message: '修改成功',
                type: 'success'
              });
            }
          })
      }, 10000)
    },
    vuex() {
      // console.log(store.state.count)
      console.log(this.$store.state.user)
    }
  },
  mounted() {
    // console.log(store.state.count)
    // 监听事件的触发
    // 'getLoginName', this.loginForm
    // bus.$on('getLoginName', (data) => {
    //   console.log("这是接收到的数据：", data)
    // })
    console.log(this.$store.state.user)
    this.$refs.stage.appendChild(canvas);
    main_1.maingame();
    //将分数初始化
    // if (this.count === 1) {
    //   this.$router.go(0);
    //   this.count++;

    //   console.log(this.count)
    // }
    // this.$router.replace({
    //   path:"nothing"
    // })
    console.log(JSON.parse(localStorage.getItem("getisFirst")))
    //判断是否为首次进入游戏,
    //如果是首次进入游戏,
    //需要进行刷新,并将生命,分数等参数跟新
    if (JSON.parse(localStorage.getItem("getisFirst")) == true) {
      location.reload();
      console.log("已刷新")
      localStorage.setItem("getisFirst", JSON.stringify("false"));
    }
    // location.reload();
    //将数据保存到本地
    console.log(JSON.parse(localStorage.getItem("insuranceCode")))
    this.player = { ...JSON.parse(localStorage.getItem("insuranceCode")) };
    // if (this.player.life == 0) {
    //   this.player.life = 3
    //   this.player.score = 0
    // }
    window.life = this.player.life
    window.score = this.player.score
    setInterval(() => {
      //当生命值小于1,即为零时,游戏重置
      if (window.life < 1) {
        // window.life = 3
        // window.score = 0;
        console.log("已重置")
        this.player.life = 3;
        this.player.score = 0;
        //这里的储存操作,是为了防止有人抽风玩着玩着点刷新
        localStorage.setItem("insuranceCode", JSON.stringify(this.player));
        this.axios.post('http://localhost:3312/sys-user/update', this.player)
        .then((resp) => {

          console.log("this is update", resp);
          let data = resp.data;
          //
          if (data.success) {
            console.log({
              message: '修改成功',
              type: 'success'
            });
          }
        })
        window.life = 3
        window.score = 0
      }
      this.player.life = window.life
      this.player.score = window.score
      console.log(this.player)
      localStorage.setItem("insuranceCode", JSON.stringify(this.player));
      console.log(this.player.life, this.player.score,window.life,window.score)
      this.axios.post('http://localhost:3312/sys-user/update', this.player)
        .then((resp) => {

          console.log("this is update", resp);
          let data = resp.data;
          //
          if (data.success) {
            console.log({
              message: '修改成功',
              type: 'success'
            });
          }
        })
    }, 1000)
  },
}

</script>
  
<style>
#stage {
  width: 480px;
  height: 650px;
  margin: 0 auto;
}
</style>
  
  